public interface Map {
 //定义方法
 public void put(Object key,Object value);
 public Object get(Object key);
 public int size();
 public boolean isEmpty();
 //定义内部接口
 interface Entry{
 public Object getKey();
 public Object getValue();
    }
}

public class SelfHashMap implements Map {
 public Entry[] table;//指向哈希表的数组引用
 int size;//键值对的数量
 public HashMap() {
 table = new Entry[11];//哈希表主数组默认长度16
 }
 public void put(Object key, Object value) {
 //计算哈希码
 int hash = key.hashCode();
 //根据哈希码计算存储位置
 int index = hash % table.length;
 //存入指定位置
 if (table[index] == null) {//如果该位置没有元素，增加一个节点
 table[index] = new Entry(hash, key, value);
 size++;
        } else {//如果有元素，进行逐个比较
 Entry entry = table[index];
 //Entry previousEntry = null;
 while (entry != null) {
 //如果找到了相同的key
 if (entry.getKey().equals(key)) {
 //新的value替代旧的value
 entry.value = value;
 return;
                }
 //previousEntry = entry;
 entry = entry.next;
            }
 //循环结束，表明也没有相同的key，在链表最后添加一个节点
 //previousEntry.next = new Entry(hash, key, value);
            //循环结束，表明也没有相同的key，在链表最前边添加一个节点
 Entry firstEntry = table[index];
 table[index] = new Entry(hash, key, value,firstEntry);
 size++;
        }
    }
 public Object get(Object key) {
 //计算哈希码
 int hash = key.hashCode();
 //根据哈希码计算存储位置
 int index = hash % table.length;
 //查找对应的Entry
 Entry entry = null;
 if (table[index] != null) {
            Entry currentEntry = table[index];
 while (currentEntry != null) {
 if (currentEntry.getKey().equals(key)) {
                    entry = currentEntry;
 break;
                }
                currentEntry = currentEntry.next;
            }
        }
 return entry == null ? null : entry.getValue();
    }
 public String toString() {
        StringBuilder builder = new StringBuilder("{");
 for(int i=0;i<table.length;i++){
 if(table[i] != null){
                Entry entry = table[i];
 while(entry != null){
             builder.append(entry.getKey()+"="+entry.getValue()+",");
                    entry = entry.next;
                }
            }
        }
 if(builder.length()!=1){
            builder.deleteCharAt(builder.length()-1);
        }
        builder.append("}");
 return builder.toString();
    }
 public int size() {
 return size;
 public boolean isEmpty() {
 return size == 0;
    }
 class Entry implements Map.Entry {
 private int hash;//哈希码
 private Object key;//键
 private Object value;//值
 private Entry next;//指向下一个节点的指针
 public Entry() {
        }
 public Entry(int hash, Object key, Object value) {
 this.hash = hash;
 this.key = key;
 this.value = value;
        }
 public Entry(int hash, Object key, Object value, Entry next) {
 this.hash = hash;
 this.key = key;
 this.value = value;
 this.next = next;
        }
 public Object getKey() {
 return key;
        }
 public Object getValue() {
 return value;
        }
 public String toString() {
 if (next != null) {
 return "{" + key + ":" + value + "-" + hash + " " + next + "}";
            } else {
 return "{" + key + ":" + value + "-" + hash + "}";
            }
        }
    }
}